﻿// test_class_inheritance.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
using namespace std;
class Base
{
public:
    Base() { cout << "Create Base " << name << endl; }
    Base(int i) { cout << i << "Create Base\n"; }
    ~Base() { cout << "Drop Base" << endl; };
    void BaseFun() { cout << "call BaseFun\n"; }
    void Test() { cout << "Base Test\n"; }
    int index{ 0 };
    int x{ 0 };
    int y{ 0 };
    string name{ "BaseName" };

protected:
    int pid{ 0 };
    void PFunc() { cout << "call PFunc\n"; }
};

class A :public Base
{
public:
    A():Base(100)
    {
        cout << Base::name << " Create A " << name << endl; 
    }
    ~A() { cout << "Drop A " << name << endl; }

    void AFunc() { cout << "A Func " << endl; }
    void Test() { cout << "A Test\n"; }
    int a_index{ 0 };
    string name{ "AName" };
};

// 保护继承 Base的public变派生类protected
class ProClass : protected Base
{
public:
    ProClass() { this->index++; }
    int pro_index{ 0 };
};

class ProClassChild : public ProClass
{
public: //可以访问Base的public和protected
    ProClassChild() { this->PFunc(); }
};

// 基类的public和protected都变成private
class PriClass : private Base
{
public:
    PriClass() { this->PFunc(); }
};

class PriClassChild :public PriClass
{
    //不能访问Base中的任何方法
    PriClassChild() { 
        //this->PFunc(); //错误
    }
};


int main()
{
    {
        A a;
        cout << "a.name: " << a.name << endl;
		cout << "a.Base::name: " << a.Base::name << endl;

        a.Test();
        a.Base::Test();
        return 0;
    }
    {
        //ProClass pc;
        //pc.pro_index;
        //return 0;
    }
    Base b;
    cout << sizeof(b) << ":" << &b << endl;
    {
        cout << "======A begin======\n";
        A a1;
        
    }
    cout << "======A end======\n";
    //A a1;
    //a1.index++;
    //派生类访问基类的成员变量和函数
    //cout << "a1.index = " << a1.index << endl;
    //cout << "a1.BaseFun():";
    //a1.BaseFun();
    //a1.AFunc();
    //a1.a_index++;
    //cout << sizeof(a1) << ":" << &a1 << endl;
    //cout << "&a1.index:" << (long long)&a1.index << endl;
    //cout << "&a1.a_index" << (long long)&a1.a_index << endl;
    //cout << "&a1:" << (long long)&a1 << endl;

}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件，或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来，若要再次打开此项目，请转到“文件”>“打开”>“项目”并选择 .sln 文件
